﻿namespace EnergyGeneratorSimVisualizer
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using EnergyGeneratorSimVisualizer.Serialization;
    using EnergyGeneratorSimVisualizer.ViewModels;
    using System.Text;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            var vm = new MainWindowViewModel();

            if (vm.LoggingOutput is ObservableCollection<string>)
            {
                ((ObservableCollection<string>)vm.LoggingOutput).CollectionChanged += this.MainWindow_CollectionChanged;
            }

            this.DataContext = vm;
            this.InitializeComponent();

            this.chartConfiguration.Update();
        }

        /// <summary>
        /// Gets the view model.
        /// </summary>
        /// <value>The view model.</value>
        protected MainWindowViewModel ViewModel
        {
            get
            {
                return this.DataContext as MainWindowViewModel;
            }
        }

        /// <summary>
        /// Handles the CollectionChanged event of the MainWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
        private void MainWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                var scrollViewerInternal = GetScrollViewer(this.loggingDisplay);

                if (scrollViewerInternal != null)
                {
                    scrollViewerInternal.ScrollToEnd();
                }
            }
        }

        /// <summary>
        /// Gets the scroll viewer.
        /// </summary>
        /// <param name="listBox">The list box.</param>
        /// <returns></returns>
        public static ScrollViewer GetScrollViewer(ListBox listBox)
        {
            if (VisualTreeHelper.GetChildrenCount(listBox) == 0)
            {
                return null;
            }

            Border scroll_border = VisualTreeHelper.GetChild(listBox, 0) as Border;
            if (scroll_border != null)
            {
                ScrollViewer scroll = scroll_border.Child as ScrollViewer;

                return scroll;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void StartSimulationClick(object sender, RoutedEventArgs e)
        {
            var vm = this.ViewModel;

            if (vm != null)
            {
                vm.CalculateSimulation();
            }
        }

        private void CancelSimulationClick(object sender, RoutedEventArgs e)
        {
            var vm = this.ViewModel;

            if (vm != null)
            {
                vm.CancelSimulation();
            }
        }

        private void OnChartChanged(IEnumerable<ChartDescription> chartDescriptions)
        {
            this.chartDisplayArea.Update(chartDescriptions);
        }

        private void SaveSettingsClick(object sender, RoutedEventArgs e)
        {
            this.ViewModel.SaveSettingsAsync();
        }

        private void LoadSettingsClick(object sender, RoutedEventArgs e)
        {
            this.ViewModel.LoadSettingsAsync();
        }
    }
}
